package com.gk.service.impl;

import com.alibaba.fastjson.JSON;
import com.gk.bean.*;
import com.gk.bean.qfdto.InStockBillDto;
import com.gk.bean.qfdto.InStockBillIndexDto;
import com.gk.dao.InStockBillDao;
import com.gk.dao.ZxyUploadbillDao;
import com.gk.dao.ZxyUploadbillGuoyaoDao;
import com.gk.http.HttpApi;
import com.gk.http.entity.HttpCommon;
import com.gk.http.entity.HttpRequestParams;
import com.gk.http.entity.HttpResult;
import com.gk.service.InBillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class InBillServiceImpl implements InBillService {
    @Autowired
    private InStockBillDao inStockBillDao;
    @Autowired
    private ZxyUploadbillGuoyaoDao zxyUploadbillGuoyaoDao;

    @Override
    public HttpResult CgBillUpload(String billCode) {
        InStockBillIndexDto indexDto = inStockBillDao.getCgBillIndexInfo(billCode);
        List<InStockBillDto> billDtos = inStockBillDao.getCgBillInfo(billCode);
        HttpRequestParams httpRequestParams = new HttpRequestParams();
        httpRequestParams.setTrackNo(billCode);
        CommonDto commonDto = new CommonDto();
        T301HeaderBean t301HeaderBean = new T301HeaderBean();
        commonDto.getXml().setHeader(t301HeaderBean);
        commonDto.getXml().getContent().getDetail().setMain(indexDto);
        for (InStockBillDto billDto : billDtos) {
            commonDto.getXml().getContent().getDetail().getSub().getSubline().add(billDto);
        }
        httpRequestParams.getData().add(commonDto);
        httpRequestParams.setTrackNo(indexDto.getPorderid());
        log.info("上传国控采购订单信息json:" + JSON.toJSONString(httpRequestParams));
        HttpResult httpResult = HttpApi.HttpRequest2(httpRequestParams, HttpCommon.INSTOCKBILL_URL);
        log.info("上传国控采购订单信息返回json:" + JSON.toJSONString(httpResult));
        //TODO 修改中间库数据
        if (httpResult.getCode().equals("0")) {
            for (InStockBillDto billDto : billDtos) {
                ZxyUploadbillGuoyao zxyUploadbillJzt = new ZxyUploadbillGuoyao();
                zxyUploadbillJzt.setBillcode(billCode);
                zxyUploadbillJzt.setOrd(Integer.parseInt(billDto.getPorderdtlid()));
                zxyUploadbillJzt.setBilltype(BillTypeCommon.buyBill);
                zxyUploadbillGuoyaoDao.updateFlags(zxyUploadbillJzt);
            }
        }
        return httpResult;
    }

    @Override
    public void CgBillUpload() {
        List<String> billCodeList = inStockBillDao.getZXY_NeedUploadBillCode("11");
        for (String billCode : billCodeList) {
            InStockBillIndexDto indexDto = inStockBillDao.getCgBillIndexInfo(billCode);
            List<InStockBillDto> billDtos = inStockBillDao.getCgBillInfo(billCode);
            HttpRequestParams httpRequestParams = new HttpRequestParams();
            httpRequestParams.setTrackNo(billCode);
            CommonDto commonDto = new CommonDto();
            T301HeaderBean t301HeaderBean = new T301HeaderBean();
            commonDto.getXml().setHeader(t301HeaderBean);
            commonDto.getXml().getContent().getDetail().setMain(indexDto);
            for (InStockBillDto billDto : billDtos) {
                commonDto.getXml().getContent().getDetail().getSub().getSubline().add(billDto);
            }
            httpRequestParams.getData().add(commonDto);
            httpRequestParams.setTrackNo(indexDto.getPorderid());
            log.info("上传国控采购订单信息json:" + JSON.toJSONString(httpRequestParams));
            HttpResult httpResult = HttpApi.HttpRequest2(httpRequestParams, HttpCommon.INSTOCKBILL_URL);
            log.info("上传国控采购订单信息返回json:" + JSON.toJSONString(httpResult));
            //TODO 修改中间库数据
//            if (httpResult.getCode().equals("0")) {
//                for (InStockBillDto billDto : billDtos) {
//                    ZxyUploadbillGuoyao zxyUploadbillJzt = new ZxyUploadbillGuoyao();
//                    zxyUploadbillJzt.setBillcode(billCode);
//                    zxyUploadbillJzt.setOrd(Integer.parseInt(billDto.getPorderdtlid()));
//                    zxyUploadbillJzt.setBilltype(34);
//                    zxyUploadbillGuoyaoDao.updateFlags(zxyUploadbillJzt);
//                }
//            }
        }
    }

    @Override
    public HttpResult XsBackBillUpload(String billCode) {
        InStockBillIndexDto indexDto = inStockBillDao.getXsBackBillIndexInfo(billCode);
        List<InStockBillDto> billDtos = inStockBillDao.getXsBackBillInfo(billCode);
        HttpRequestParams httpRequestParams = new HttpRequestParams();
        CommonDto commonDto = new CommonDto();
        T301HeaderBean t301HeaderBean = new T301HeaderBean();
        commonDto.getXml().setHeader(t301HeaderBean);
        commonDto.getXml().getContent().getDetail().setMain(indexDto);
        for (InStockBillDto billDto : billDtos) {
            commonDto.getXml().getContent().getDetail().getSub().getSubline().add(billDto);
        }
        httpRequestParams.getData().add(commonDto);
        httpRequestParams.setTrackNo(indexDto.getPorderid());
        log.info("上传国控销售退货信息json:" + JSON.toJSONString(httpRequestParams));
        HttpResult httpResult = HttpApi.HttpRequest2(httpRequestParams, HttpCommon.INSTOCKBILL_URL);
        log.info("上传国控销售退货信息返回json:" + JSON.toJSONString(httpResult));
        //TODO 修改中间库数据
        if (httpResult.getCode().equals("0")) {
            for (InStockBillDto billDto : billDtos) {
                ZxyUploadbillGuoyao zxyUploadbillJzt = new ZxyUploadbillGuoyao();
                zxyUploadbillJzt.setBillcode(billCode);
                zxyUploadbillJzt.setOrd(Integer.parseInt(billDto.getPorderdtlid()));
                zxyUploadbillJzt.setBilltype(BillTypeCommon.saleBackBill);
                zxyUploadbillGuoyaoDao.updateFlags(zxyUploadbillJzt);
            }
        }
        return httpResult;
    }

    @Override
    public HttpResult calcelOrederBill(String billcode, int jobFlag, int billtype) {
        return null;
    }
}
